﻿@model GameSchool.Models.ViewModels.CourseEditViewModel

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="../../Scripts/jquery.tmpl.min.js" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Áfangi</legend>
        
        @Html.HiddenFor(model => model.Course.ID)

        <div class="editor-label">
            <p>Nafn</p>
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Course.Name)
        </div>

        <div class="editor-label">
            <p>Lýsing</p>
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Course.Description)
        </div>

        @if (Model.Course.ID != 0)
        {
            <div class="editor-field">
                <table id="Students" class="UserTable">
                    <caption class="UserTableNames">@Html.LabelFor(model => model.Students)</caption>
                    <thead>
                        <tr>
                            <th>Nafn</th>
                            <th>Bæta í áfanga</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var student in Model.Students)
                        {
                            if (Model.StudentNameInCourse.Contains(student.UserName))
                            {
                                <tr>
                                    <td>@student.UserName</td>
                                    <td id="@student.UserName"><a href="#" class="student_link" onclick="RemoveStudentFromCourse(@Model.Course.ID, '@student.UserName')">-</a></td>
                                </tr>
                            }
                            else
                            {
                                <tr>
                                    <td>@student.UserName</td>
                                    <td id="@student.UserName"><a href="#" class="student_link" onclick="SetStudentToCourse(@Model.Course.ID, '@student.UserName')">+</a></td>
                                </tr>
                            }
                        }
                    </tbody>
                </table>
            </div>
        

            <script type="text/html" id="student_plus_template">
                <a href="#" class="student_link" onclick="SetStudentToCourse(${CourseID}, '${StudentUsername}')">+</a>
            </script>

            <script type="text/html" id="student_minus_template">
                <a href="#" class="student_link" onclick="RemoveStudentFromCourse(${CourseID},'${StudentUsername}')">-</a>
            </script>
        
            <script type="text/html" id="student_error_template">
                <p id="error">Nemandinn þegar í sex áföngum</p>
            </script>

            <div class="editor-field">
                <table id="Teachers" class="UserTable">
                    <caption class="UserTableNames">@Html.LabelFor(model => model.Teachers)</caption>
                    <thead>
                        <tr>
                            <th>Nafn</th>
                            <th>Bæta í áfanga</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var teacher in Model.Teachers)
                        {
                            if (Model.TeachersNameInCourse.Contains(teacher.UserName))
                            {
                                <tr>
                                    <td>@teacher.UserName</td>
                                    <td id="@teacher.UserName">Kennari áfangans. Ekki hægt að fjarlægja. Vinsamlegast hafðu samband á mail : tindur11@ru.is</td>
                                </tr>
                            }
                            else
                            {
                                 <tr>
                                    <td>@teacher.UserName</td>
                                    <td id="@teacher.UserName"><a href="#" class="teacher_link" onclick="SetTeacherToCourse(@Model.Course.ID, '@teacher.UserName')">+</a></td>
                                </tr>
                            }
                        }
                    </tbody>
                </table>
            </div>

            <script type="text/html" id="teacher_plus_template">
                <a id="link_ ${UserName}" href="#" onclick="SetTeacherToCourse(${CourseID}, '${UserName}')">+</a>
            </script>

            <script type="text/html" id="teacher_minus_template">
                <a id="link_ ${UserName}" href="#" onclick="RemoveTeacherFromCourse(${CourseID}, '${UserName}')">-</a>
            </script>
            
            <script type="text/html" id="teacher_error_template">
                <a id="error">Áfanginn er núþegar með kennara</a>
            </script>
        }
        <p>
            <input class="SaveChanges" type="submit" value="Save" />
        </p>
    </fieldset>
    
        
}

<div>
    @Html.ActionLink("Til Baka", "GetCourses")
</div>

<script type="text/javascript">

    function SetStudentToCourse(CourseID, UserName) {
        $.post("/Admin/AddStudentToCourse", { CourseID: CourseID, StudentName: UserName }, function (data) {
            if (data != "") {
                $("#" + UserName + "> a").remove();
                $("#student_minus_template").tmpl(data).appendTo("#" + UserName);
            }
            else {
                $("#student_error_template").tmpl(data).appendTo("#" + UserName);
                $("#error").fadeOut(5000, function () {
                    $("#error").remove();
                });
            }
        });
    }
    function RemoveStudentFromCourse(CourseID, UserName) {
        $.post("/Admin/RemoveStudentFromCourse", { CourseID: CourseID, StudentName: UserName }, function (data) {
            $("#" + UserName + "> a").remove();
            $("#student_plus_template").tmpl(data).appendTo("#" + UserName);
        });
    }
    function SetTeacherToCourse(CourseID, UserName) {
        $.post("/Admin/AddTeacherToCourse", { CourseID: CourseID, TeachersUserName: UserName }, function (data) {
            if (data != "") {
                $("#" + UserName + "> a").remove();
                $("#teacher_minus_template").tmpl(data).appendTo("#" + UserName);
            }
            else {
                $("#teacher_error_template").tmpl(data).appendTo("#" + UserName);
                $("#error").fadeOut(5000, function () {
                    $("#error").remove();
                }); 
            }
        });
    }
       /* $.post("/Admin/AddTeacherToCourse", { CourseID: CourseID, TeachersUserName: UserName })
        $("#" + UserName + "> a").remove();
        $("#teacher_minus_template").tmpl(data).appendTo("#" + UserName);
    }
    function RemoveTeacherFromCourse(CourseID, UserName) {
        $.post("/Admin/RemoveTeacherFromCourse", { CourseID: CourseID, TeachersUserName: UserName }, function (data) {
            $("#" + UserName + "> a").remove();
            $("#teacher_plus_template").tmpl(data).appendTo("#" + UserName);
        });
    }*/
</script>